<link rel="import" href="../../../../packages/polymer/polymer.html">
<link rel="import" href="class_ref.html">
<link rel="import" href="error_view.html">
<link rel="import" href="eval_box.html">
<link rel="import" href="eval_link.html">
<link rel="import" href="field_ref.html">
<link rel="import" href="function_ref.html">
<link rel="import" href="inbound_reference.html">
<link rel="import" href="instance_ref.html">
<link rel="import" href="observatory_element.html">
<link rel="import" href="nav_bar.html">
<link rel="import" href="object_common.html">
<link rel="import" href="context_ref.html">
<link rel="import" href="view_footer.html">

<polymer-element name="instance-view" extends="observatory-element">
  <template>
    <link rel="stylesheet" href="css/shared.css">
    <nav-bar>
      <top-nav-menu></top-nav-menu>
      <vm-nav-menu vm="{{ instance.isolate.vm }}"></vm-nav-menu>
      <isolate-nav-menu isolate="{{ instance.isolate }}"></isolate-nav-menu>
      <!-- TODO(turnidge): Add library nav menu here. -->
      <class-nav-menu cls="{{ instance.clazz }}"></class-nav-menu>
      <nav-menu link="." anchor="instance" last="{{ true }}"></nav-menu>
      <nav-refresh callback="{{ refresh }}"></nav-refresh>
    </nav-bar>

    <template if="{{ instance.isError }}">
      <error-view error_obj="{{ instance['error'] }}"></error-view>
    </template>

    <template if="{{ !instance.isError }}">
      <div class="content">
        <template if="{{ instance.isAbstractType }}">
          <h1>type {{ instance.name }}</h1>
        </template>
        <template if="{{ !instance.isAbstractType }}">
          <h1>instance of {{ instance.clazz.name }}</h1>
        </template>

        <object-common object="{{ instance }}"></object-common>

        <div class="memberList">
          <div class="memberItem">&nbsp;</div>

          <template if="{{ instance.valueAsString != null }}">
            <div class="memberItem">
              <div class="memberName">value</div>
              <div class="memberValue">
                <pre>{{ instance.valueAsString }}</pre>
              </div>
            </div>
          </template>

          <template if="{{ instance.isString }}">
            <div class="memberItem">
              <div class="memberName">valueAsLiteral</div>
              <div class="memberValue"> {{ asStringLiteral(instance.valueAsString, instance.valueAsStringIsTruncated) }}</div>
            </div>
          </template>

          <template if="{{ instance.isMirrorReference }}">
            <div class="memberItem">
              <div class="memberName">referent</div>
              <div class="memberValue">
                <any-service-ref ref="{{ instance.referent }}">
                </any-service-ref>
              </div>
            </div>
          </template>

          <template if="{{ instance.typeClass != null }}">
            <div class="memberItem">
              <div class="memberName">type class</div>
              <div class="memberValue">
                <class-ref ref="{{ instance.typeClass }}">
                </class-ref>
              </div>
            </div>
          </template>

          <template if="{{ instance.isClosure }}">
            <div class="memberItem">
              <div class="memberName">closure function</div>
              <div class="memberValue">
                <function-ref ref="{{ instance.function }}">
                </function-ref>
              </div>
            </div>
            <div class="memberItem">
              <div class="memberName">closure context</div>
              <div class="memberValue">
                <any-service-ref ref="{{ instance.context }}">
                </any-service-ref>
              </div>
            </div>
          </template>

          <template if="{{ instance.isWeakProperty }}">
            <div class="memberItem">
              <div class="memberName">key</div>
              <div class="memberValue">
                <any-service-ref ref="{{ instance.key }}">
                </any-service-ref>
              </div>
            </div>
            <div class="memberItem">
              <div class="memberName">value</div>
              <div class="memberValue">
                <any-service-ref ref="{{ instance.value }}">
                </any-service-ref>
              </div>
            </div>
          </template>

          <div class="memberItem">
            <div class="memberName">toString()</div>
            <div class="memberValue">
              <eval-link callback="{{ evaluate }}" expr="toString()"></eval-link>
            </div>
          </div>
        </div>
      </div>

      <hr>

      <div class="content">
        <eval-box callback="{{ evaluate }}"></eval-box>
      </div>

      <hr>

      <div class="content">
        <template if="{{ instance.nativeFields.isNotEmpty }}">
          native fields ({{ instance.nativeFields.length }})
          <curly-block expand="{{ instance.nativeFields.length <= 100 }}">
            <div class="memberList">
              <template repeat="{{ field in instance.nativeFields }}">
                <div class="memberItem">
                  <div class="memberName">[{{ field['index']}}]</div>
                  <div class="memberValue">[{{ field['value']}}]</div>
                </div>
              </template>
            </div>
          </curly-block><br><br>
        </template>

        <template if="{{ instance.fields.isNotEmpty }}">
          fields ({{ instance.fields.length }})
          <curly-block expand="{{ instance.fields.length <= 100 }}">
            <div class="memberList">
              <template repeat="{{ field in instance.fields }}">
                <div class="memberItem">
                  <div class="memberName">
                    <field-ref ref="{{ field['decl'] }}"></field-ref>
                  </div>
                  <div class="memberValue">
                    <any-service-ref ref="{{ field['value'] }}"></any-service-ref>
                  </div>
                </div>
              </template>
            </div>
          </curly-block><br><br>
        </template>

        <template if="{{ instance.elements.isNotEmpty }}">
          elements ({{ instance.elements.length }})
          <curly-block expand="{{ instance.elements.length <= 100 }}">
            <div class="memberList">
              <template repeat="{{ index in instance.elements.asMap().keys }}">
                <div class="memberItem">
                  <div class="memberName">[{{ index }}]</div>
                  <div class="memberValue">
                    <any-service-ref ref="{{ instance.elements[index] }}">
                    </any-service-ref>
                  </div>
                </div>
              </template>
            </div>
          </curly-block><br><br>
        </template>

        <template if="{{ instance.associations.isNotEmpty }}">
          associations ({{ instance.elements.length }})
          <curly-block expand="{{ instance.associations.length <= 100 }}">
            <div class="memberList">
              <template repeat="{{ association in instance.associations }}">
                <div class="memberItem">
                  <div class="memberValue">
                    [<any-service-ref ref="{{ association['key'] }}"></any-service-ref>]
                  </div>
                  <div class="memberValue">
                    <any-service-ref ref="{{ association['value'] }}"></any-service-ref>
                  </div>
                </div>
              </template>
            </div>
          </curly-block><br><br>
        </template>

      </div>

    </template>
    <view-footer></view-footer>
  </template>
</polymer-element>

<script type="application/dart" src="instance_view.dart"></script>
